package com.molyfun.parents.view;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.adapter.BaseAdapter;
import com.molyfun.parents.impl.IHorizontalTag;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * 底部横条的TabBar
 */
public class HorizontalTagBar<T> extends RecyclerView {

    protected LayoutInflater layoutInflater;

    private int mChooseTab = 0;

    private Context context;

    LinearLayoutManager mLinearLayoutManager;
    private HorizontalTagAdapter mHorizontalTagAdapter;

    private List<T> mTabsList;

    public HorizontalTagBar(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public HorizontalTagBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public HorizontalTagBar(Context context) {
        super(context);
        init(context, null);
    }

    public void initDataList(List<T> list){
        this.mTabsList = list;
        mHorizontalTagAdapter.refreshData(list);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        mLinearLayoutManager = new LinearLayoutManager(context);
        mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        setLayoutManager(mLinearLayoutManager);
//        layoutInflater = LayoutInflater.from(context);
//        layoutInflater.inflate(R.layout.widget_attachment, this);
        setHasFixedSize(true);

        mHorizontalTagAdapter = new HorizontalTagAdapter(context);
        setAdapter(mHorizontalTagAdapter);
    }

    private OnHorizontalTagBarClickListener mOnHorizontalTagBarClickListener;

    public void setOnHorizontalTagBarClickListener(OnHorizontalTagBarClickListener onHorizontalTagBarClickListener){
        this.mOnHorizontalTagBarClickListener = onHorizontalTagBarClickListener;
    }

    public interface OnHorizontalTagBarClickListener{
        public void onTagBarClick(int position);
    }

    public int getCurrentChooseTab(){
        return mChooseTab;
    }


    public class HorizontalTagAdapter extends BaseAdapter<T> {

        public HorizontalTagAdapter(Context context) {
            super(context);
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new HorizontalTagViewHolder(LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.layout_list_horizontal_tag_cell, parent, false));
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, final int position) {
            HorizontalTagViewHolder horizontalTagViewHolder = (HorizontalTagViewHolder)holder;
            horizontalTagViewHolder.mTagTabText.setText(((IHorizontalTag)mTabsList.get(position)).getName());

            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)horizontalTagViewHolder.mTagTabText.getLayoutParams();
            if(position == 0){
                layoutParams.setMargins(context.getResources().getDimensionPixelOffset(R.dimen.common_padding), 0, context.getResources().getDimensionPixelOffset(R.dimen.file_cell_margin_right), 0);
            }else{
                layoutParams.setMargins(0, 0, context.getResources().getDimensionPixelOffset(R.dimen.file_cell_margin_right), 0);
            }

            if(mOnHorizontalTagBarClickListener != null){
                horizontalTagViewHolder.mTagTabText.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnHorizontalTagBarClickListener.onTagBarClick(position);
                    }
                });
            }
        }

        public class HorizontalTagViewHolder extends ViewHolder{

            @InjectView(R.id.tag_tab_text)
            TextView mTagTabText;

            public HorizontalTagViewHolder(View view) {
                super(view);
                ButterKnife.inject(this, view);
            }

        }
    }


}